package org.cybzacg.convert.converters.numeric;

import org.cybzacg.convert.annotation.Converter;
import org.cybzacg.convert.context.ConversionContext;
import org.cybzacg.convert.enums.ConversionError;
import org.cybzacg.convert.enums.ConversionPriority;
import org.cybzacg.convert.enums.ConversionType;
import org.cybzacg.convert.exception.ConversionException;
import lombok.extern.slf4j.Slf4j;

/**
 * 浮点型转换器
 * 专门处理float和Float类型的转换
 *
 * @author cybzacg.blog
 * @version 4.0
 * @since 3.0
 */
@Slf4j
@Converter(
    name = "FloatConverter",
    priority = ConversionPriority.HIGH,
    description = "浮点型转换器，专门处理float和Float类型的转换",
    enabled = true,
    version = "4.0",
    author = "cybzacg.blog",
    supportedTypes = {Float.class, float.class},
    tags = {"float", "numeric", "basic", "conversion"}
)
public class FloatConverter extends AbstractNumericConverter<Float> {

    public FloatConverter() {
        super(Float.class);
    }

    @Override
    protected ConversionType getConversionType() {
        return ConversionType.FLOAT;
    }

    @Override
    protected void validateRange(Number sourceNumber, Object result, ConversionContext context)
            throws ConversionException {

        if (sourceNumber instanceof Double) {
            double value = sourceNumber.doubleValue();
            if (value < -Float.MAX_VALUE || value > Float.MAX_VALUE) {
                throw new ConversionException(ConversionError.NUMBER_OVERFLOW,
                    String.format("数值超出Float范围: %f (范围: %f 到 %f)",
                        value, -Float.MAX_VALUE, Float.MAX_VALUE));
            }
        }
    }
}
